package com.lloyd.animdemo

import android.graphics.Path
import android.os.Bundle
import android.os.Handler
import android.os.Looper
import android.os.Message
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Surface
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.tooling.preview.Preview
import androidx.recyclerview.widget.RecyclerView
import com.lloyd.animdemo.pathlayoutmanager.PathLayoutManager
import com.lloyd.animdemo.ui.theme.AnimDemoTheme

class MainActivity : ComponentActivity() {

    // 是否从右到左布局
    private val isRTL = false

    private var mPathLayoutManager: PathLayoutManager? = null
    private lateinit var adapter: DemoAdapter

    // 记录当前位置
    private var currPos = 0

    private val duration = 3000L

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        init()
    }

    private fun init() {
        val list = mutableListOf<String>()
        for (i in 1..100) list.add("test$i")
        adapter = DemoAdapter(list)

        val recyclerView = findViewById<RecyclerView>(R.id.recycler_view)
        val path = buildScrollPath()
        mPathLayoutManager = PathLayoutManager(path, 150)
        mPathLayoutManager?.setItemScaleRatio(0f, 0f, 1f, 0.5f, 0f, 1f)
        mPathLayoutManager?.setItemAlphaRatio(0f, 0f, 1f, 0.5f, 0f, 1f)
        mPathLayoutManager?.setItemDirectionFixed(true)
        mPathLayoutManager?.setAutoSelect(true)
        mPathLayoutManager?.setFlingEnable(true)
        mPathLayoutManager?.setItemOffset(680)
        mPathLayoutManager?.setScrollMode(PathLayoutManager.SCROLL_MODE_LOOP)
        mPathLayoutManager?.setOnItemSelectedListener { position ->
            currPos = position
            scrollToNext()
        }
        recyclerView.layoutManager = mPathLayoutManager
        recyclerView.adapter = adapter
    }

    // 构建贝塞尔路径
    private fun buildScrollPath(): Path {
        // 屏幕宽高
        val screenHeight = 1920F
        val screenWidth = 720F
        val path = Path()
        if (isRTL) {
            //设置起点
            path.moveTo(0f, 400f)
            //二阶贝塞尔。
            path.quadTo(screenWidth - 400f, 300f, screenWidth - 400f, 800f)
            path.quadTo(screenWidth - 400f, 1500f, 0f, screenHeight - 280)
        } else {
            //设置起点
            path.moveTo(screenWidth, 400f)
            //二阶贝塞尔。
            path.quadTo(400f, 300f, 400f, 800f)
            path.quadTo(400f, 1500f, screenWidth, screenHeight - 280)
        }
        return path
    }

    private fun scrollToNext() {
        currPos += 1
        handler.sendEmptyMessageDelayed(handlerMessageSignal, duration)
    }

    private val handlerMessageSignal = 1
    private val handler = object : Handler(Looper.getMainLooper()) {
        override fun handleMessage(msg: Message) {
            super.handleMessage(msg)
            if (msg.what == handlerMessageSignal) {
                mPathLayoutManager?.smoothScrollToPosition(currPos % (adapter.size()))
            }
        }
    }
}